package es

import (
	"context"
	"fmt"
	"github.com/olivere/elastic/v7"
)


type LogData struct {
	Topic string `json:"topic"`
	Data string `json:"data"`
}
var (
	client *elastic.Client
	esChan  =make( chan *LogData,100)
)


func EsInit(addr string) (err error) {
	client, err = elastic.NewClient(elastic.SetURL("http://"+addr))
	if err != nil {
		return err
	}
	fmt.Println("connect to es success")
	go sendToEs()
	return nil
}

func SendToEs(msg *LogData)  {
 	esChan<-msg
}

func sendToEs(){
	for data:=range esChan{
		sd, err := client.Index().
			Index(data.Topic).Type("abc").
			BodyJson(data).
			Do(context.Background())
		if err != nil {
			// Handle error
			panic(err)
		}
		fmt.Println(sd)
	}


}
